var Info = app.getUserGlobalInfo();
var table_tby = document.getElementById("table-tby");
$.ajax({
  url: app.ServerUrl + '\n' +
    'v1/repository/getSelfEliminateRequests',
  type: "get",
  contentType: "application/json;charset=UTF-8",
  dataType: "json",
  async: true,
  headers: {
    "Authorization": "460bcdb0-b326-4d6d-ac99-232c83ba6d80"
  },
  data: {
    userId: Info.userId
  },
  success: function (data) {
    // console.log(data)
    var result = data.objs[0];
    // localStorage.setItem("singleInfo", JSON.stringify(result));
    var html = '';
    for (let i = 0; i < result.length; i++) {
      var singleInfo = result[i];
      if (singleInfo.status == 0) {
        var inTime = app.getFormatDate(singleInfo.requestTime);
        html +=
          `<tr> 
                <td> ${singleInfo.num !=null? singleInfo.num:singleInfo.serialNum}  </td> 
                <td> ${singleInfo.categoryName} </td> 
                <td>  ${singleInfo.typicalName}  </td> 
                <td>  ${singleInfo.brand}  </td> 
                <td>  ${singleInfo.typicalModel} </td> 
                <td>  ${inTime}  </td>
                <td><span style="color: red;">暂未处理</span></td>
                <td style="text-align: center"> 
                <button type="button" class="layui-btn layui-btn-danger layui-btn-sm" onclick="CancelItem(this,'${singleInfo.saleId}')"> 
                撤销申请
                </button> 
                </td> 
                </td>
                </tr>`
      } else if (singleInfo.status == 1) {
        var inTime = app.getFormatDate(singleInfo.requestTime);
        html += `<tr> 
                <td> ${singleInfo.num !=null? singleInfo.num:singleInfo.serialNum}  </td>
                <td> ${singleInfo.categoryName} </td> 
                <td>  ${singleInfo.typicalName}  </td> 
                <td>  ${singleInfo.brand}  </td> 
                <td>  ${singleInfo.typicalModel} </td> 
                <td>  ${inTime}  </td>
                <td><span style="color: red;">已通过</span></td>
                <td  style="text-align: center">
                <button type="button" class="layui-btn layui-btn-danger layui-btn-sm" onclick="DeleteItem(this,'${singleInfo.saleId}')"> 
                删除记录
                </button> 
                </td>
                </tr>`
      } else if (singleInfo.status == 2) {
        var inTime = app.getFormatDate(singleInfo.requestTime);
        html += `<tr> 
                <td> ${singleInfo.num !=null? singleInfo.num:singleInfo.serialNum}  </td>
                <td> ${singleInfo.categoryName} </td> 
                <td>  ${singleInfo.typicalName}  </td> 
                <td>  ${singleInfo.brand}  </td> 
                <td>  ${singleInfo.typicalModel} </td> 
                <td>  ${inTime}  </td>
                <td><span style="color: red;">已驳回</span></td>
                <td  style="text-align: center">
                <button type="button" class="layui-btn layui-btn-danger layui-btn-sm" onclick="DeleteItem(this,'${singleInfo.saleId}')"> 
                删除记录
                </button> 
                </td>
                </tr>`
      }
      table_tby.innerHTML = html;
    }
  },
  error: function (e) {
    layer.msg('操作失败！');
  }
})

// 撤销申请
function CancelItem(e, saleId) {
  layer.open({
    title: '提示信息',
    content: '确认要撤销该申请吗？',
    btn: ['确定', '取消'],
    yes: function (index) {
      $.ajax({
        url: app.ServerUrl + '\n' +
          'v1/repository/cancelEliminateRequest',
        type: "get",
        contentType: "application/json;charset=UTF-8",
        dataType: "json",
        async: true,
        headers: {
          "Authorization": "460bcdb0-b326-4d6d-ac99-232c83ba6d80"
        },
        data: {
          saleId: saleId
        },
        success: function (data) {
          // console.log(data)
          if (data.resultStatus == 1) {
            layer.msg('销库申请撤销成功！', {
              icon: 1,
              time: 1000
            }, function () {
              setTimeout(function () {
                $(e).parent().parent().remove();
              }, 1000)
            });
          } else {
            layer.msg('销库申请撤销失败！');
          }
        },
        error: function (e) {
          layer.msg('操作失败！');
        }
      })
    },
    cancel: function () {}
  })
}

// 删除申请
function DeleteItem(e, saleId) {
  layer.open({
    title: '提示信息',
    content: '确认要删除该申请吗？',
    btn: ['确定', '取消'],
    yes: function (index) {
      $.ajax({
        url: app.ServerUrl + '\n' +
          'v1/repository/deleteEliminateRequest',
        type: "get",
        contentType: "application/json;charset=UTF-8",
        dataType: "json",
        async: true,
        headers: {
          "Authorization": "460bcdb0-b326-4d6d-ac99-232c83ba6d80"
        },
        data: {
          saleId: saleId
        },
        success: function (data) {
          // console.log(data)
          if (data.resultStatus == 1) {
            layer.msg('删除成功！', {
              icon: 1,
              time: 2000
            }, function () {
              setTimeout(function () {
                $(e).parent().parent().remove();
              }, 1000)
            });
          } else {
            layer.msg('操作失败！')
          }
        },
        error: function (e) {
          layer.msg('操作失败！');
        }
      })
    },
    cancel: function () {}
  })
}